﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace AliveCard {
public class TouchManager : MonoBehaviour {
    int LayerMask_Room = 0;
    // Use this for initialization
    void Awake() {
        LayerMask_Room = 1 << LayerMask.NameToLayer("touch");
    }

    // Update is called once per frame
    void Update() {
#if UNITY_EDITOR || UNITY_STANDALONE_WIN
        //DebugUtil.Log(Input.GetTouch(0).position);
        if(Input.GetMouseButtonDown(0)) {
            Vector2 touchPos = Input.mousePosition;
            TouchRaycast(touchPos);
        }
#elif UNITY_ANDROID
        if(Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began) {
            Vector2 touchPos = Input.GetTouch(0).position;
            TouchRaycast(touchPos);
        }
#endif

    }

    // 数字越大，优先级越高
    enum TouchPriority {
        TouchPriority_Min,
        TouchPriority_Room,
        TouchPriority_Hero,
        TouchPriority_NPC,
    }

    // 数字越大，优先级越高
    int Tag2TouchPriority(string tag) {
        if(tag == "room") {
            return (int)TouchPriority.TouchPriority_Room;
        } else if(tag == "npc") {
            return (int)TouchPriority.TouchPriority_NPC;
        } else if(tag == "hero") {
            return (int)TouchPriority.TouchPriority_Hero;
        } else {
            return (int)TouchPriority.TouchPriority_Min;
        }
    }


    Vector3 Debug_TouchPos;

    void TouchRaycast(Vector2 touchPos) {
        Debug_TouchPos = touchPos;
        Ray ray = Camera.main.ScreenPointToRay(touchPos);
        RaycastHit2D[] hitInfos;
        hitInfos = Physics2D.RaycastAll(Camera.main.ScreenToWorldPoint(touchPos), Vector2.zero, 0f, LayerMask_Room);
        if(hitInfos != null) {
            Transform touchTF = null;
            int curPrio = -1;
            //点击优先级
            foreach(var hitInfo in hitInfos) {
                if(hitInfo.collider != null) {
                    Transform tmpTouchTF = hitInfo.collider.transform.parent;
                    int tmpPrio = Tag2TouchPriority(tmpTouchTF.tag);
                    if(tmpPrio > curPrio) {
                        curPrio = tmpPrio;
                        touchTF = tmpTouchTF;
                    }

                }
            }

            if(touchTF != null) {
                DebugUtil.LogFormat("[TouchManager] You pressed obj. obj:[{0}]", touchTF.name);

                if(touchTF.tag == "room") {
                    Room room = touchTF.GetComponent<Room>();
                    if(room != null) {
                        RoomManager.Ins.MoveToRoom(room);
                    }
                } else if(touchTF.tag == "npc" || touchTF.tag == "hero") {
                    ACPuppet p = touchTF.GetComponent<ACPuppet>();
                    if(p != null) {
                        p.OnTouched();
                    }
                }
            }
        }
    }

    private void OnDrawGizmos() {
        if(Debug_TouchPos != Vector3.zero) {
            Gizmos.color = Color.green;
            Gizmos.DrawWireSphere(Debug_TouchPos, 0.1f);
            Gizmos.color = Color.blue;
            Gizmos.DrawWireSphere(Camera.main.ScreenToWorldPoint(Debug_TouchPos), 0.1f);
        }
    }
}
}
